package edu.ccut.saturn.studio.saturndata.dialog;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.component.IValidateInfo;
import edu.ccut.saturn.component.impl.SaturnValidateUseInfo;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.saturndata.model.DataItem;
import edu.ccut.saturn.studio.saturndata.model.JavaTypeUtils;


/**
 * 编辑数据库表对话框
 * 
 * @author ma teng yue
 * 
 */
public class DataItemAddByCustemDialog extends TitleAreaDialog {

	private Text datetimeFormatText;
	private Label datetimeFormatLabel;
	private Text numberFormatText;
	private Label numberFormatLabel;
	private Label nameLabel, lengthLabel, javaTypeLabel, showNameLabel,
			defaultValueLabel;

	private Text nameText, lengthText, showNameText, defaultValueText;

	private Combo javaTypeCombo;

	private DataItem dataItem;

	public DataItem getDataItem() {
		return dataItem;
	}

	public DataItemAddByCustemDialog(Shell parentShell) {
		super(parentShell);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("DataItemAddByCustemDialog.addduserdefinedfield"));  //$NON-NLS-1$
	}

	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		// 设置标题
		setTitle(Messages.getString("DataItemAddByCustemDialog.userdefinedfield")); //$NON-NLS-1$
		// 设置对话框样式效果
		return contents;
	}

	// 初始化界面大小
	protected Point getInitialSize() {
		return new Point(486, 320);
	}

	protected Control createDialogArea(Composite parent) {
		getShell().setText(Messages.getString("DataItemAddByCustemDialog.addduserdefinedfield"));  //$NON-NLS-1$
		Composite compositeall = new Composite(parent, SWT.NONE);
		compositeall.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 2, 1));
		GridLayout gridlayout = new GridLayout();
		gridlayout.marginLeft = 30;
		gridlayout.marginWidth = gridlayout.marginHeight = 2;
		gridlayout.verticalSpacing = 7;
		gridlayout.horizontalSpacing = 3;
		gridlayout.numColumns = 2;
		compositeall.setLayout(gridlayout);

		nameLabel = new Label(compositeall, SWT.NONE);
		nameLabel.setText(Messages.getString("DataItemAddByCustemDialog.name"));  //$NON-NLS-1$

		nameText = new Text(compositeall, SWT.SINGLE | SWT.BORDER | SWT.NULL);
		nameText.setLayoutData(new GridData(363, SWT.DEFAULT));
		nameText.setTextLimit(50);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		lengthLabel = new Label(compositeall, SWT.NONE);
		lengthLabel.setText(Messages.getString("DataItemAddByCustemDialog.length"));  //$NON-NLS-1$

		lengthText = new Text(compositeall, SWT.SINGLE | SWT.BORDER);
		lengthText.setTextLimit(4);
		lengthText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false));
		lengthText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		javaTypeLabel = new Label(compositeall, SWT.NONE);
		javaTypeLabel.setText(Messages.getString("DataItemAddByCustemDialog.javatype")); //$NON-NLS-1$
		javaTypeCombo = new Combo(compositeall, SWT.READ_ONLY);

		javaTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false));
		javaTypeCombo.setText(Messages.getString("DataItemAddByCustemDialog.java.lang.String"));  //$NON-NLS-1$
		javaTypeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		javaTypeCombo.setItems(JavaTypeUtils.getSelectedJavaType());

		showNameLabel = new Label(compositeall, SWT.NONE);
		showNameLabel.setText(Messages.getString("DataItemAddByCustemDialog.showname"));  //$NON-NLS-1$

		showNameText = new Text(compositeall, SWT.SINGLE | SWT.BORDER
				| SWT.NULL);
		showNameText.setTextLimit(50);
		showNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false));
		showNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		defaultValueLabel = new Label(compositeall, SWT.NONE);
		defaultValueLabel.setText(Messages.getString("DataItemAddByCustemDialog.defaultvalue"));  //$NON-NLS-1$

		defaultValueText = new Text(compositeall, SWT.SINGLE | SWT.BORDER
				| SWT.NULL);
		defaultValueText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				false, false));

		datetimeFormatLabel = new Label(compositeall, SWT.NONE);
		datetimeFormatLabel.setText(Messages.getString("DataItemAddByCustemDialog.dateformat"));  //$NON-NLS-1$

		datetimeFormatText = new Text(compositeall, SWT.BORDER);
		final GridData gd_datetimeFormatText = new GridData(SWT.FILL,
				SWT.CENTER, false, false);
		gd_datetimeFormatText.widthHint = 354;
		datetimeFormatText.setLayoutData(gd_datetimeFormatText);

		numberFormatLabel = new Label(compositeall, SWT.NONE);
		numberFormatLabel.setText(Messages.getString("DataItemAddByCustemDialog.numberformat"));  //$NON-NLS-1$

		numberFormatText = new Text(compositeall, SWT.BORDER);
		final GridData gd_numberFormatText = new GridData(SWT.FILL, SWT.CENTER,
				false, false);
		gd_numberFormatText.widthHint = 352;
		numberFormatText.setLayoutData(gd_numberFormatText);

		setMessage(Messages.getString("DataItemAddByCustemDialog.shownamelengthlimit")); //$NON-NLS-1$
		return compositeall;
	}

	// 添加完成按钮
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	/**
	 * 用 SWT.RESIZE、SWT.MAX、SWT.MIN 分别设置窗口为可变大小、可最大化、可最小化
	 */
	protected int getShellStyle() {
		return super.getShellStyle() | SWT.MIN | SWT.MAX | SWT.RESIZE;
	}

	private void dialogChanged() {
		String name = nameText.getText().trim();
		String length = lengthText.getText().trim();
		
		if (SaturnValidation.isEmpty(name)) {
			if ("".equals(name) && !"".equals(length)){
				setErrorMessage(Messages.getString("DataItemAddByCustemDialog.NameCannotBeEmpty"));   //$NON-NLS-1$
				getButton(IDialogConstants.OK_ID).setEnabled(false);
			} else if ("".equals(name)) {
				setErrorMessage(Messages.getString("DataItemAddByCustemDialog.NameCannotBeEmpty"));   //$NON-NLS-1$
			}
			return;
		} 
		
		if (!SaturnValidation.isValidProgramStr(name)) {
			setErrorMessage(Messages.getString("DataItemAddByCustemDialog.NameLimit"));  //$NON-NLS-1$
			getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}
		
		if (!SaturnValidation.isInt(length)) {
			setErrorMessage(Messages.getString("DataItemAddByCustemDialog.lengthconstitue"));  //$NON-NLS-1$
			getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		} 
		
		if (SaturnValidation.isEmpty(length)) {
			setErrorMessage(Messages.getString("DataItemAddByCustemDialog.LengthLimit"));    //$NON-NLS-1$
			super.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		} 
		if (SaturnValidation.isEmpty(javaTypeCombo.getText().trim())) {
			setErrorMessage(Messages.getString("DataItemAddByCustemDialog.choosejavatype"));   //$NON-NLS-1$
			getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		} 
		if(!SaturnValidation.isValidProgramStr(showNameText.getText().trim()) && !SaturnValidation.isGB2312(showNameText.getText().trim())) {
			setErrorMessage(Messages.getString("DataItemAddByCustemDialog.shownameconstitute"));  //$NON-NLS-1$
			getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}
		
		if (SaturnValidation.isEmpty(name)) {
			setErrorMessage(Messages.getString("DataItemAddByCustemDialog.NameCannotBeEmpty"));   //$NON-NLS-1$
			getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		} 
		
			setErrorMessage(null);
			this.getButton(IDialogConstants.OK_ID).setEnabled(true);	
			
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.CANCEL_ID) {
			close();
			super.buttonPressed(buttonId);
		}
		if (buttonId == IDialogConstants.OK_ID ) {
			this.dataItem = new DataItem();

			String name = nameText.getText().trim();
			String length = lengthText.getText().trim();
			String javatype = javaTypeCombo.getText().trim();
			String showName = showNameText.getText().trim();
			String defaultValue = defaultValueText.getText().trim();
			String datetimeFormat = datetimeFormatText.getText().trim();
			String numberFomrat = numberFormatText.getText().trim();

			this.dataItem.setName(name);
			this.dataItem.setLength(length);
			this.dataItem.setJavaType(javatype);
			this.dataItem.setShowName(showName);
			this.dataItem.setDefaultValue(defaultValue);
			this.dataItem.setNumberFormat(numberFomrat);
			this.dataItem.setDatetimeFormat(datetimeFormat);

			this.dataItem.setValidateUseInfo(new SaturnValidateUseInfo(name,
					javatype, false, new ArrayList<IValidateInfo>()));
			super.buttonPressed(buttonId);
		}
	
	}
}
